﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SQLite;
using System.Drawing;

namespace HOOMT
{
    class OriginalDiagramDAO : BaseDAO
    {
        public bool AddDiagram(OriginalDiagram diagram)
        {


            try
            {
                this.DeleteDiagram(diagram);
                //   this.conn = this.GetConnection();
                SQLiteCommand cmd = this.GetCmd();
                cmd.CommandText = "insert into OriginalDiagram( id ,x ,y,width,height,name,isSelected,newModelFormId,treeNode,diagramtype) "
                   + " values (@id,@x,@y,@width,@height,@name,@isSelected,@newModelFormId,@treeNode,@diagramtype)";
                cmd.Parameters.Add(new SQLiteParameter("id", diagram.Id));
                cmd.Parameters.Add(new SQLiteParameter("x", diagram.UpLeftpoint.X));
                cmd.Parameters.Add(new SQLiteParameter("y", diagram.UpLeftpoint.Y));
                cmd.Parameters.Add(new SQLiteParameter("width", diagram.Width));
                cmd.Parameters.Add(new SQLiteParameter("height", diagram.Height));
                cmd.Parameters.Add(new SQLiteParameter("name", diagram.Name));
                cmd.Parameters.Add(new SQLiteParameter("isSelected", false));



                if ("HighOrderObjectDiagram".Equals(this.GetSimpleType(diagram.GetType().ToString())))
                {
                    if (((HighOrderObjectDiagram)diagram).HasModelForm)
                    {
                        cmd.Parameters.Add(new SQLiteParameter("newModelFormId", ((HighOrderObjectDiagram)diagram).NewModelForm.id));
                    }
                    else
                    {
                        cmd.Parameters.Add(new SQLiteParameter("newModelFormId", null));
                    }
                }
                else if ("HighStateDiagram".Equals(this.GetSimpleType(diagram.GetType().ToString())))
                {
                    if (((HighStateDiagram)diagram).HasModelForm)
                    {
                        cmd.Parameters.Add(new SQLiteParameter("newModelFormId", ((HighStateDiagram)diagram).NewModelForm.id));
                    }
                    else
                    {
                        cmd.Parameters.Add(new SQLiteParameter("newModelFormId", null));
                    }
                }
                else if ("InformationSourceDiagram".Equals(this.GetSimpleType(diagram.GetType().ToString())))
                {
                    if (((InformationSourceDiagram)diagram).HasModelForm)
                    {
                        cmd.Parameters.Add(new SQLiteParameter("newModelFormId", ((InformationSourceDiagram)diagram).NewModelForm.id));
                    }
                    else
                    {
                        cmd.Parameters.Add(new SQLiteParameter("newModelFormId", null));
                    }
                }


                else
                {
                    cmd.Parameters.Add(new SQLiteParameter("newModelFormId", null));
                }


                if (diagram.treeNode != null)
                {
                    cmd.Parameters.Add(new SQLiteParameter("treeNode", diagram.treeNode.FullPath));
                }
                else
                {
                    cmd.Parameters.Add(new SQLiteParameter("treeNode", null));
                }


                cmd.Parameters.Add(new SQLiteParameter("diagramtype", diagram.GetType().ToString()));

                //向数据库添加Method和Attribute
                if (diagram is ObjectDiagram)
                {
                    MethodDAO mdbo = new MethodDAO();
                    AttributeDAO adbo = new AttributeDAO();
                    foreach (Method m in ((ObjectDiagram)diagram).MethodList)
                    {
                        mdbo.AddMethod(m, diagram.Id);

                    }

                    foreach (Attribute attr in ((ObjectDiagram)diagram).AttributeList)
                    {
                        adbo.AddAttribute(attr, diagram.Id);
                    }
                }


                cmd.ExecuteNonQuery();
                this.CloseConnection();
                return true;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
            finally
            {
                CloseConnection();
            }


        }
        public bool DeleteDiagram(OriginalDiagram diagram)
        {
            try
            {
                SQLiteCommand cmd = this.GetCmd();
                cmd.CommandText = "delete from OriginalDiagram where id=@id";
                cmd.Parameters.Add(new SQLiteParameter("id", diagram.Id));
                //同时删除关联的Method和Attr
                Console.WriteLine("diagram is" + diagram.GetType());
                if (diagram is ObjectDiagram)
                {
                    Console.WriteLine("DELETE OK");
                    MethodDAO mdbo = new MethodDAO();
                    AttributeDAO adbo = new AttributeDAO();
                    foreach (Method m in ((ObjectDiagram)diagram).MethodList)
                    {
                        mdbo.DeleteMethod(m);

                    }

                    foreach (Attribute attr in ((ObjectDiagram)diagram).AttributeList)
                    {
                        adbo.DeleteAttribute(attr);
                    }
                }

                cmd.ExecuteNonQuery();
                return true;
            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return false;
            }
            finally
            {
                CloseConnection();
            }
        }

        public OriginalDiagram GetDiagramById(String id)
        {
            try
            {
                SQLiteCommand cmd = this.GetCmd();
                cmd.CommandText = "select * from OriginalDiagram where id=@id";
                cmd.Parameters.Add(new SQLiteParameter("id", id));
                SQLiteDataReader reader = cmd.ExecuteReader();
                if (reader.Read())
                {
                    //  OriginalDiagram diagram=null;

                    String diagramType = reader["diagramType"].ToString();
                    String name = reader["name"].ToString();


                    int x = int.Parse(reader["x"].ToString());
                    int y = int.Parse(reader["y"].ToString());
                    int width = int.Parse(reader["width"].ToString());
                    int height = int.Parse(reader["height"].ToString());
                    string newModelFormId = reader["newModelFormId"].ToString();
                    //1
                    if ("HighOrderObjectDiagram".Equals(this.GetSimpleType(diagramType)))
                    {
                        //  Console.WriteLine("22222222");
                        HighOrderObjectDiagram diagram = new HighOrderObjectDiagram(
                        new Point(x + width / 2, y + height / 2), width, height);
                        diagram.Id = id;
                        diagram.Name = name;
                        // diagram.treeNode=
                        //添加方法
                        MethodDAO mdbo = new MethodDAO();
                        diagram.MethodList = mdbo.GetMethodList(diagram);

                        //      Console.WriteLine("!!!!methodlist length is " + mdbo.GetMethodList(diagram).Count);

                        //添加属性
                        AttributeDAO adbo = new AttributeDAO();
                        diagram.AttributeList = adbo.GetAttributeList(diagram);
                        //添加newModelFormId
                        diagram.newModelFormId = newModelFormId;
                        return diagram;
                    }
                    //2
                    else if ("HighStateDiagram".Equals(this.GetSimpleType(diagramType)))
                    {
                        HighStateDiagram diagram = new HighStateDiagram(
                         new Point(x + width / 2, y + height / 2), width, height);

                        diagram.Name = name;
                        diagram.Id = id;

                        //添加newModelFormId
                        diagram.newModelFormId = newModelFormId;
                        return diagram;
                    }
                    //3
                    else if ("InformationSourceDiagram".Equals(this.GetSimpleType(diagramType)))
                    {
                        InformationSourceDiagram diagram = new InformationSourceDiagram(
                        new Point(x + width / 2, y + height / 2), width, height);
                        diagram.Name = name;
                        diagram.Id = id;

                        //添加newModelFormId
                        diagram.newModelFormId = newModelFormId;
                        return diagram;
                    }
                    //4
                    //                     else if ("ObjectDiagram".Equals(this.GetSimpleType(diagramType)))
                    //                     {
                    //                         ObjectDiagram diagram = new ObjectDiagram(
                    //                         new Point(int.Parse(reader["x"].ToString()), int.Parse(reader["y"].ToString()))
                    //                         , int.Parse(reader["width"].ToString()), int.Parse(reader["height"].ToString()));
                    //                         return diagram;
                    //                     }
                    //4
                    else if ("PrimitiveObjectDiagram".Equals(this.GetSimpleType(diagramType)))
                    {
                        PrimitiveObjectDiagram diagram = new PrimitiveObjectDiagram(
                         new Point(x + width / 2, y + height / 2), width, height);
                        diagram.Name = name;
                        diagram.Id = id;

                        //添加方法
                        MethodDAO mdbo = new MethodDAO();
                        diagram.MethodList = mdbo.GetMethodList(diagram);

                        //添加属性
                        AttributeDAO adbo = new AttributeDAO();
                        diagram.AttributeList = adbo.GetAttributeList(diagram);

                        return diagram;
                    }
                    //5
                    else if ("PrimitiveStateDiagram".Equals(this.GetSimpleType(diagramType)))
                    {
                        PrimitiveStateDiagram diagram = new PrimitiveStateDiagram(
                         new Point(x + width / 2, y + height / 2), width, height);
                        diagram.Name = name;

                        diagram.Id = id;
                        return diagram;
                    }
                    //6
                    else if ("ProcessDiagram".Equals(this.GetSimpleType(diagramType)))
                    {

                        ProcessDiagram diagram = new ProcessDiagram(
                            new Point(x + width / 2, y + height / 2), width, height);

                        diagram.Name = name;
                        diagram.Id = id;

                        return diagram;
                    }
                    //8
                    //                     else if ("StateDiagram".Equals(this.GetSimpleType(diagramType)))
                    //                     {
                    //                         StateDiagram diagram = new StateDiagram(
                    //                         new Point(int.Parse(reader["x"].ToString()), int.Parse(reader["y"].ToString()))
                    //                         , int.Parse(reader["width"].ToString()), int.Parse(reader["height"].ToString()));
                    //                         return diagram;
                    //                     }
                    else
                    {
                        return null;
                    }

                }
                else
                {
                    return null;
                }

            }
            catch (System.Exception ex)
            {
                Console.WriteLine(ex.ToString());
                return null;
            }
            finally
            {
                CloseConnection();
            }
        }
    }
}
